package icegps.com.pingdj.activity;

import android.app.Activity;
import android.bluetooth.BluetoothGatt;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.clj.fastble.data.BleDevice;
import com.clj.fastble.exception.BleException;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

import icegps.com.pingdj.App;
import icegps.com.pingdj.R;
import icegps.com.pingdj.bean.DeviceStatus;
import icegps.com.pingdj.bean.LocationStatus;
import icegps.com.pingdj.bean.SatelliteData;
import icegps.com.pingdj.data.analysis.AnalysisCallback;
import icegps.com.pingdj.data.analysis.AnalysisHelper;
import icegps.com.pingdj.data.ble.BleHelper;
import icegps.com.pingdj.data.ble.BleStatusCallback;

/**
 * Created by 111 on 2018/3/8.
 */

public abstract class BaseActivity extends AppCompatActivity implements BleStatusCallback, AnalysisCallback {
    public Activity activity;
    private RelativeLayout rootView;
    private TextView tvTitle;
    private TextView tvLocationStatus;
    private TextView tvDelay;
    private TextView tvSatelliteSum;
    private static int satelliteSum;
    private static int delay;
    private static boolean isConnect;
    public static int status;
    private Unbinder bind;
    public DeviceStatus deviceStatus;

    private TextView tvDis;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DeviceStatus.init();
        deviceStatus = DeviceStatus.getDeviceStatus();
        activity = this;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_base);
        findView();
        tvTitle.setText(setTitle());
        View inflate = View.inflate(activity, setLayout(), null);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        rootView.addView(inflate, 0, params);
        bind = ButterKnife.bind(this);
        init();
    }

    private void findView() {
        rootView = findViewById(R.id.root_view);
        tvDis = findViewById(R.id.tv_dis);
        tvTitle = findViewById(R.id.tv_title);
        tvLocationStatus = findViewById(R.id.tv_location_status);
        tvDelay = findViewById(R.id.tv_delay);
        tvSatelliteSum = findViewById(R.id.tv_satellite_sum);
    }

    protected abstract String setTitle();

    protected abstract void init();


    protected abstract int setLayout();

    @Override
    protected void onResume() {
        super.onResume();
        refStatus();
        BleHelper.getInstance().addBleStatusCallback(this);
        AnalysisHelper.getInstance().addAnalysisHelper(this);
        boolean connect = BleHelper.getInstance().isConnect();
        if (!connect) {
            isConnect = false;
            satelliteSum = 0;
            status = 0;
            refStatus();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleHelper.getInstance().removeBleStatusCallback(this);
        AnalysisHelper.getInstance().removeAnalysisHelper(this);
        App.getInstance().getDaoSession().getDeviceStatusDao().insertOrReplace(DeviceStatus.getDeviceStatus());
        bind.unbind();
    }

    public void startActivity(Class clazz) {
        startActivity(new Intent(this, clazz));
    }


    protected void refStatus() {
        if (isConnect) {
            tvDis.setVisibility(View.GONE);
        } else {
            tvDis.setVisibility(View.VISIBLE);
        }
        if (status == 4) {
            tvLocationStatus.setText("可用");
        } else {
            tvLocationStatus.setText("不可用");
        }
        tvDelay.setText(delay + "s");
        tvSatelliteSum.setText(String.valueOf(satelliteSum));
    }


    //设备信息回调start
    public void onAnalysisHexLocationData(LocationStatus locationStatus) {
        status = locationStatus.getStatus();
        delay = (int) locationStatus.getDelay();
        satelliteSum = locationStatus.getSatelliteSum();
        refStatus();
    }

    @Override
    public void onAnalysisHexSatelliteData(ArrayList<SatelliteData> satelliteDatas, byte type) {

    }

    @Override
    public void onAnalysisAscStringData(String data) {

    }

    public void onAnalysisAscDeviceStatus(DeviceStatus deviceStatus) {

    }
    //设备信息回调end


    //蓝牙状态回调start
    @Override
    public void onScanStarted(boolean success) {

    }

    @Override
    public void onScanning(BleDevice result) {

    }

    @Override
    public void onScanFinished(List<BleDevice> scanResultList) {

    }

    @Override
    public void onStartConnect() {

    }

    @Override
    public void onConnectFail(BleException exception) {

    }

    @Override
    public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {

    }

    @Override
    public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {
        isConnect = false;
        BaseActivity.status = 0;
        delay = 0;
        satelliteSum = 0;
        refStatus();
    }

    @Override
    public void onNotifySuccess() {
        isConnect = true;
        refStatus();
    }

    @Override
    public void onNotifyFailure(BleException exception) {

    }

    @Override
    public void onCharacteristicChanged(byte[] data) {

    }

    @Override
    public void onWriteSuccess() {

    }

    @Override
    public void onWriteFailure(BleException exception) {

    }

    @Override
    public void onBleOpen() {

    }

    @Override
    public void onBleClose() {

    }
    //蓝牙状态回调end


    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

}
